
/**
 * 
 *
 * 根据不同类型获取附件
 * vue必须要定义为vm  要有files列表
 */
function getFiles(doctype,docid) {
	var url = baseUrl + "com.itc.oa.app.subfile.query.biz.ext";
	//var doctype = "wf";
	var param = {
		doctype: doctype,
		docid: docid,
		tokenid: userInfo.tokenId(),
		timeStamp: (new Date()).valueOf()
	}
	apiManage.get(url, param, function(ret) {
		DebugPrint({
			"获取文件返回": ret
		});
		//console.log("文件列表:" + JSON.stringify(ret.data));
		vm.files = ret.data;
		refreshFileStates();

	}, function(err) {
		//hui.upToast(JSON.stringify(err));
		//联网失败的回调,隐藏下拉刷新和上拉加载的状态;
		console.log("获取文件列表失败:" + JSON.stringify(err));
	});
}

function checkFile(file) {
	//查询文件是否存在
	var local_image_url = "_downloads/work/" + file.fileId;

	//var absolute_image_path = plus.io.convertLocalFileSystemURL(local_image_url); // 平台绝对路径
	console.log("文件地址:" + local_image_url);

	plus.io.resolveLocalFileSystemURL(local_image_url, function(fs) {
		console.log("文件存在");
		openFile(local_image_url);
	}, function(err) {
		//alert("获取文件失败!" + err.message );
		console.log("文件不存在");
		downloadFile(file);
	});
}

function downloadFile(file) {
	fileSize = file.itcfFSubfiles[0].fileSize;
	var fileUrl = baseUrl + "com.itc.oa.app.download.flow?fileId=" + file.fileId +
		"&tokenid=" + userInfo.tokenId();
	hui.loading("正在下载附件...")
	console.log("文件下载地址:" + fileUrl);
	var saveName = "_downloads/work/" + file.fileId;
	var dtask = plus.downloader.createDownload(fileUrl, {
		filename: saveName
	}, function(d, status) {
		// 下载完成
		if (status == 200) {
			//alert("Download success: " + d.filename);
			hui.closeLoading();
			console.log("文件下载成功:" + d.filename);
			refreshFileStates();
			openFile(d.filename);
		} else {
			hui.closeLoading();
			alert("Download failed: " + status);
		}
	});
	dtask.addEventListener("statechanged", onStateChanged, false);
	dtask.start();
}

var precent = 0;
var fileSize = 0;

function onStateChanged(download, status) {
	if (download.state == 3) {
		var pre = Math.ceil(download.downloadedSize / fileSize * 100);
		console.log("downloading: " + download.downloadedSize + "|" + fileSize);
		if (pre - precent >= 1) {
			console.log("downloading: " + pre + "%");
			hui.loading("正在下载附件..." + pre + "%")
		}
		precent = pre;
	}
	if (download.state == 4 && status == 200) {
		console.log("download success: " + download.responseText);
	}
}

function openFile(filePath) {
	console.log("打开文件:" + filePath);

	plus.runtime.openFile(filePath, {}, function(err) {
		alert("打开文件失败:" + err.message);
	});
}

function refreshFileStates() {
	if (!window.plus) {
		return;
	}

	vm.files.forEach(function(f, index) {
		var local_image_url = "_downloads/work/" + f.fileId;
		plus.io.resolveLocalFileSystemURL(local_image_url, function(fs) {
			console.log("文件已存在:" + local_image_url);
			vm.$set(vm.files[index], "isExit", true);
		}, function(err) {
			console.log("文件不存在:" + local_image_url);
			vm.$set(vm.files[index], "isExit", false);
		});
	});
}
